// pages/readWorks/addWork/index.js
var app = getApp();
var dateTimePicker = require('../../../js/dateTimePicker.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tasktype: '', //任务类型
    publisher: '',// 发布人
    publishtime: '', // 任务开始时间
    requirements: '', //任务要求
    daylimit: '', //任务时限（天）
    material: '', //所需资料
    totalscore: '', //完成后所得总积分
    disabled: false,
    loading: false,
    selectObject: [],
    selectstr: '',
    today: '',
    dateTimeArray: null,
    dateTime: null // 默认数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const param = dateTimePicker.dateTimePicker('2020','2050')
    const dateTimeArray = param.dateTimeArray
    const dateTime = param.dateTime
    this.setData({
      dateTimeArray: param.dateTimeArray,
      dateTime: dateTime,
      tasktype: options.tasktype
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  publisherinput(e){
    this.setData({
      publisher: e.detail.value
    })
  },
  publishtimeinput(e){
    const dateTime = e.detail.value
    const dateTimeArray = this.data.dateTimeArray
    const publishtime = dateTimeArray[0][dateTime[0]]+'-'+dateTimeArray[1][dateTime[1]]+'-'+dateTimeArray[2][dateTime[2]]+' '+dateTimeArray[3][dateTime[3]]+':'+dateTimeArray[3][dateTime[3]]
    this.setData({
      publishtime: publishtime
    })
  },
  requirementsinput(e){
    this.setData({
      requirements: e.detail.value
    })
  },
  daylimitinput(e){
    if (e.detail.value) {
      if (!(/(^\+?[1-9][0-9]*$)/.test(e.detail.value))) {
        wx.showModal({
          title: '失败',
          content:"任务时限只能输入正整数",
          showCancel: !1,
        });
        this.setData({
          daylimit: ""
        })
        return;
      }
    }
    this.setData({
      daylimit: e.detail.value
    })
  },
  materialinput(e){
    this.setData({
      material: e.detail.value
    })
  },
  totalscoreinput(e){
    if (e.detail.value) {
      if (!(/(^\+?[1-9][0-9]*$)/.test(e.detail.value))) {
        wx.showModal({
          title: '失败',
          content:"完成后所得总积分只能输入正整数",
          showCancel: !1,
        });
        this.setData({
          totalscore: ""
        })
        return
      }
    }
    this.setData({
      totalscore: e.detail.value
    })
  },

  // 确定提交
  surePublish () {
    const that = this
    const publisher = this.data.publisher
    if (publisher == '') {
      wx.showModal({
        title: '友情提示',
        content: '请填写发布人姓名!',
        showCancel: !1,
      })
      return;
    }

    const publishtime = this.data.publishtime
    if (publishtime == '') {
      wx.showModal({
        title: '友情提示',
        content: '请选择任务开始时间!',
        showCancel: !1,
      })
      return;
    }

    const requirements = this.data.requirements
    if (requirements == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入任务完成要求!',
        showCancel: !1,
      })
      return;
    }

    const daylimit = this.data.daylimit
    if (daylimit == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入完成任务时限（天）!',
        showCancel: !1,
      })
      return;
    }

    const material = this.data.material
    if (material == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入任务所需印证资料!',
        showCancel: !1,
      })
      return;
    }

    const totalscore = this.data.totalscore

    const objectArr = this.data.selectObject
    if (objectArr.length <= 0) {
      wx.showModal({
        title: '友情提示',
        content: '请选择接收任务对象!',
        showCancel: !1,
      })
      return;
    }
    var selectObject = JSON.stringify(objectArr)
    
    wx.showLoading({
      title: '提交中',
    })
    this.setData({
      disabled: true,
      loading: true
    })
    const tasktype = this.data.tasktype
    const usid = wx.getStorageSync('usid');
    app.wxpost({
      url: 'issuedWorks/insertPuhlishwork.html',
      data: {
        publisher: publisher,
        publishtime: publishtime,
        requirements: requirements,
        daylimit: daylimit,
        material: material,
        totalscore: totalscore,
        selectObject: selectObject,
        tasktype: tasktype,
        usid: usid
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          disabled: false,
          loading: false
        })
        wx.redirectTo({
          url: '/pages/readWorks/listWorks/index'
        })
      }
    })
  },


  selectObject () {
    var nj = wx.getStorageSync('nj');
    if (nj && nj != '0') {
      wx.navigateTo({
        url: '../../readWorks/nonBjObject/index'
      })
    }else{
      wx.navigateTo({
        url: '../../readWorks/nonNjobject/index'
      })
    }
  }

})